/*
 * MIT License
 *
 * Copyright (c) 2020 wen.gu <454727014@qq.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

/***************************************************************************
 * Name: uds_transport.h
 *
 * Purpose: implementation unix domain socket transport(dialer and listener) ops for cbus
 *
 * Developer:
 *   wen.gu , 2022-03-01
 *
 * TODO:
 *
 ***************************************************************************/

/******************************************************************************
 **    INCLUDES
 ******************************************************************************/
#ifndef __UDS_TRANSPORT_H__
#define __UDS_TRANSPORT_H__

#include <stdio.h>
#include <sys/socket.h>
#include <sys/select.h>
#include <unistd.h>
//#include <netinet/in.h>
#include <sys/un.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <string.h>
#include <stdint.h>

#include "cbus/core/cbus_type.h"
#include "cbus/core/cbus_error.h"
#include "cbus/bus/cbus_transport.h"
/******************************************************************************
 **    MACROS
 ******************************************************************************/

#define MAX_CLIENT_CONNECT_LIMIT 128

/** is the size of socket receive cache buffer */
#define SOCK_RECV_SIZE 1500

/** how many second will be block when call select */
#define SELECT_TIME_OUT_SEC 1

/** todo, refine me?? */
#define MAX_TRY_CONNECT_COUNT 10

#define UDS_TRANSPORT_URL_REFIX "ipc://"
#define UDS_TRANSPORT_URL_PREFIX_LEN 6

#ifndef INVALID_SOCKET
#define INVALID_SOCKET (-1)
#endif

#ifndef NO_ERROR
#define NO_ERROR (0)
#endif

#ifndef SOCKET_ERROR
#define SOCKET_ERROR (-1)
#endif
/******************************************************************************
 **    TYPE DEFINITIONS
 ******************************************************************************/
G_C_HEADER_START

#ifdef __x86_64__ 
typedef GS64 socket_t;
#else
typedef GS32 socket_t;
#endif
/******************************************************************************
 **    CLASSES/FUNCTIONS DEFINITIONS
 ******************************************************************************/

/**
 * @brief destroy a pipe instance
 * @param handle [in] a instance of pipe
 * @return GErrc, success: G_OK, else error code.
 * 
 */
GErrc uds_pipe_finish(GPHD handle);

/**
 * @brief send data to remote side
 * @param handle [in] the isntance handle of pipe
 * @param buf [in] data buffer
 * @param len [in] data length
 * @return G_OK: success, else: failed @see GErrc 
 * 
 */
GErrc uds_pipe_send(GPHD handle, GBUF buf, GU32 len);

/**
 * @brief receive data from remote side
 * @param handle [in] the isntance handle of pipe
 * @param buf [in] the data buffer to storage received data
 * @param buf_size [in] the size of data buffer(indicat max receive size)
 * @param timeout_ms [in] how many time to wait receive. if receive nothing in 'timeout_ms', then return G_ErrTimeout, 
 *                        CBUS_TRANSPORT_RECEIVE_WAIT_FOREVER for wait forever
 * @return > 0 : receive data size, else(<= 0): error code @see GErrc
 * 
 */

GS32 uds_pipe_recv(GPHD handle, GBUF buf, GU32 buf_size, GU32 timeout_ms);


/**
 * @brief create and initialize a dialer instance
 * @param handle [out] a instance of dialer
 * @return GErrc, success: G_OK, else error code.
 * 
*/
GErrc uds_dialer_initialize(GPHD* handle);

/**
 * @brief destroy a dialer instance
 * @param handle [in] a instance of dialer
 * @return GErrc, success: G_OK, else error code.
 * 
 */
GErrc uds_dialer_finish(GPHD handle);

/**
 * @brief connect to a listener(service)
 * @param handle     [in] a instance of dialer
 * @param url        [in] the url adress of listener(service)
 * @param timeout_ms [in] how many time to wait connect to a listener. if not connect to a listener in 'timeout_ms', then return G_ErrTimeout, 
 *                        CBUS_TRANSPORT_CONNECT_WAIT_FOREVER for wait forever
 * @param pipe_handle [out] if success output pipe instance handle, esle NULL 
 * @return GErrc, success: G_OK, else error code.
 * 
 */
GErrc uds_dialer_connect(GPHD handle, const GSTR url, GU32 timeout_ms, GPHD* pipe_handle);



////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
 * @brief create and initialize a listener instance
 * @param handle [out] the instance of listener(service)
 * @param opaque [in] a oapque handle of user set to listener for callback function
 * @param on_dialer_status [in] a callback to observe the status of dialer which connec/disconnect
 *                          to/from current listener
 * @return GErrc, success: G_OK, else error code.
 * 
 */
GErrc uds_listener_initialize(GPHD* handle);

/**
 * @brief destroy a listener instance 
 * @param handle [in] the instance handle of listener
 * @return GErrc, success: G_OK, else error code.
 *  
 */
GErrc uds_listener_finish(GPHD handle);

/**
 * @brief offer a listener(service) with url address 
 * @param handle [in] the instance handle of listener
 * @param url [in] the url address of a listener(service)
 * @return GErrc, success: G_OK, else error code.
 */
GErrc uds_listener_bind(GPHD handle, GSTR url);

/**
 * @brief stop a listner(service)
 * @param handle [in] the instance handle of listener
 * @return GErrc, success: G_OK, else error code.
 */
GErrc uds_listener_unbind(GPHD handle);


/**
 * @brief accept a connected dialer
 * @param handle [in] the instance handle of listener
 * @param dialer_id [out] the id of dialer which connected to current listener
 * @param timeout_ms [in] the time of timeout in milli-second, if not accept a dialer in 'timeout_ms', then return G_ErrTimeout, 
 *                        CBUS_TRANSPORT_ACCEPT_WAIT_FOREVER for wait forever
 * @return GErrc, success: G_OK, else error code.
 * 
 */
GErrc uds_listener_accept(GPHD handle, GPHD* pipe_handle, GU32 timeout_ms);

G_C_HEADER_END



#endif /** !__UDS_TRANSPORT_H__ */

